-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Request for Consistent Handler Restoration on expectOutputString
and expectOutputRegex
Failure
#5842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5842 +/- ##
============================================
+ Coverage 89.28% 92.32% +3.03%
+ Complexity 6585 6555 -30
============================================
Files 693 699 +6
Lines 19937 19774 -163
============================================
+ Hits 17801 18256 +455
+ Misses 2136 1518 -618 ☔ View full report in Codecov by Sentry. |
da21665
to
4dddc8c
Compare
1bb9097
to
5af6639
Compare
@KentarouTakeda could you please double check your reported issue still exist? if it still does not produce the expected outcome, please provide describe in more detail why/what does not work. thank you |
@staabm Let me explain the details. Please take a look at the test code I first presented. The minimal reproduction code above demonstrates the issue, but this behavior also affects feature tests in many frameworks, including Laravel and Symfony, producing unintended test result reports for users. For example, in Laravel, running the following test code: <?php
namespace Tests\Feature;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
// Important note: Here, `TestCase` extends the Laravel Framework's `TestCase`.
class ExampleTest extends TestCase
{
#[Test]
public function intentionalFailure()
{
$this->expectOutputString('This test will be failed'); // Failure (intentional)
}
#[Test]
#[DataProvider('repeatProvider')]
public function tenSuccessfulTests()
{
$this->assertTrue(true);
}
public static function repeatProvider()
{
return array_map(fn() => [], range(1, 10));
}
} produces the following result: FRRRRRRRRRR 11 / 11 (100%) While the first test fails as expected, it is not expected that all subsequent tests are marked as Risky. The detailed output includes messages like: There was 1 risky test:
1) Tests\Feature\ExampleTest::tenSuccessfulTests
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own
* Test code or tested code removed error handlers other than its own
* Test code or tested code removed exception handlers other than its own As shown above, in frameworks that manipulate error or exception handlers (including Laravel and Symfony), a failure in tests involving input/output can lead to user confusion due to misleading test reports. This pull request fixes the problem where the failure of one test causes handler state inconsistencies that propagate to subsequent tests, resulting in them being incorrectly marked as Risky. |
Dear Sebastian Bergmann,
This pull request addresses an issue where error and exception handlers are not restored properly when
expectOutputString
orexpectOutputRegex
assertions fail. Below is a minimal code example to reproduce the behavior:Although this may not be a "bug" per se, the inconsistent behavior makes it challenging for some frameworks to avoid PHPUnit's warning messages like "Test code or tested code ..." . It would be greatly appreciated if this issue could be addressed in PHPUnit to ensure consistent handler restoration.
Thank you for considering this request.
Best regards,
Kentaro Takeda